<template>
    <div>
        <el-form :inline="true" class="demo-form-inline" :model="cForm">
            <el-form-item label="一级分类">
                <el-select value="" placeholder="请选择" v-model="cForm.category1Id" @change="handler1" :disabled="show">
                    <el-option :label="c1.name" :value="c1.id" v-for="(c1, index) in list1" :key='c1.id'></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select value="" placeholder="请选择" v-model="cForm.category2Id" @change="handler2" :disabled="show">
                    <el-option :label="c2.name" :value="c2.id" v-for="(c2, index) in list2" :key='c2.id'></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select value="" placeholder="请选择"  v-model="cForm.category3Id" @change="handler3" :disabled="show">
                    <el-option :label="c3.name" :value="c3.id" v-for="(c3, index) in list3" :key='c3.id'></el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: 'CategorySelect',
    data() {
        return {
            list1: [],       //一级分类的数据
            list2: [],       //二级分类的数据
            list3: [],       //三级分类的数据
            cForm: {
                category1Id: '',
                category2Id: '',
                category3Id: '',
            },
        };
    },
    props:['show'],
    mounted() {
        //挂载完毕获取一级分类数据
        this.getCategory1List();
    },
    methods: {
        //获取一级分类数据的方法
        async getCategory1List() {
            //获取一级分类数据请求，不需要数据
            let result = await this.$API.attr.reqCategory1List();
            if (result.code==200) {
            this.list1 = result.data;  
            }
        },
        //一级分类的回调（根据一级分类获取二级分类数据）
        async handler1() {
            //清空数据
            this.list2=[];
            this.list3=[];
            this.cForm.category2Id='';
            this.cForm.category3Id='';
            //解构赋值
            const { category1Id } = this.cForm;

            //传递一级id给父组件(传递一个对象)
            this.$emit('getCategoryId', {categoryId:category1Id,level:1});

            //通过一级分类的id，获取二级分类的数据
            let result = await this.$API.attr.reqCategory2List(category1Id);
            if (result.code==200) {
            this.list2 = result.data;    
            }    
        },
        //二级分类的回调（根据二级分类获取三级分类数据）
        async handler2() {
            //清空数据
            this.list3=[];
            this.cForm.category3Id='';

            //解构赋值
            const { category2Id } = this.cForm;

            //传递二级id给父组件(传递一个对象)
            this.$emit('getCategoryId', {categoryId:category2Id,level:2});

            //通过二级分类的id，获取三级分类的数
            let result = await this.$API.attr.reqCategory3List(category2Id);
            if (result.code==200) {
            this.list3 = result.data;    
            }    
        },
        //三级分类的回调
        handler3(){
            const { category3Id } = this.cForm;
            //传递三级id给父组件(传递一个对象)
            this.$emit('getCategoryId', {categoryId:category3Id,level:3});
        }
    },
};
</script>

<style lang="scss" scoped>

</style>